<template>
	<view class="page">
		<view style="width: 100%;height: 25rpx;"></view>
		<view class="search-bar">
			<uni-icons type="search" size="20"></uni-icons>
			<input type="text" placeholder="搜索门店名称" v-model="searchText" />
		</view>
		<scroll-view scroll-y="true" class="printer-list" @scrolltolower="loadMore">
			<view class="printer-item" v-for="(printer, index) in filteredPrinters" :key="index"
				@tap="goAddStorePrinter(printer,2)">
				<view class="">
					<view class="printer-name">{{ printer.title }}</view>
					<view class="store-name">{{ printer.relation_store.store_name}}</view>
				</view>
				<uni-icons type="arrowright" size="20"></uni-icons>
			</view>
		</scroll-view>
		<view class="empty" v-if="isEmpty">
			<image src="/static/empty-icon.png" mode="widthFix"></image>
			<text>暂无数据，如有问题，请联系客服</text>
		</view>
	</view>
</template>

<script>
	import {
		printerList
	} from "@/api/userApi/user.js"
	export default {
		data() {
			return {
				searchText: '',
				printers: [],
				params: {
					page: 1,
					page_size: 5,
					print_brand_id: ''
				},
				isEmpty: false,
				totalCount: 0,
				pageStatus: 1
			};
		},
		computed: {
			filteredPrinters() {
				if (this.searchText) {
					return this.printers.filter(printer => printer.relation_store.store_name.includes(this.searchText));
				}
				return this.printers;
			}
		},
		methods: {
			getStoresList() {
				printerList(this.params).then(res => {
					if (res.code == 200) {
						if (res.data.list.length > 0) {
							this.isEmpty = false
						} else {
							this.isEmpty = true
						}
						this.totalCount = res.data.total
						if (this.params.page === 1) {
							this.printers = res.data.list
						} else {
							this.printers = this.printers.concat(res.data.list);
						}
					}
				})
			},
			goAddStorePrinter(val, type) {
				if (this.pageStatus == 1) {
					uni.navigateTo({
						url: `/pagesUser/addStorePrinter/addStorePrinter?val=${JSON.stringify(val)}&type=${type}`
					})
				}
				if (this.pageStatus == 2) {
					uni.navigateTo({
						url: `/pagesUser/addClerk/addClerk?val=${JSON.stringify(val)}`
					})
				}
			},
			loadMore() {
				if (this.params.page * this.params.page_size >= this.totalCount) {
					return;
				}
				this.params.page++;
				this.getStoresList();
			},
		},
		onLoad(option) {
			const pages = getCurrentPages();
			const prevPage = pages[pages.length - 2];
			const prevPageRoute = prevPage.route;
			if (prevPageRoute == 'pagesUser/printer/printer') {
				this.params.print_brand_id = option.id
				this.getStoresList()
				this.pageStatus = 1
			}
			if (prevPageRoute == 'pages/user/user') {
				this.getStoresList()
				this.pageStatus = 2
			}
		}
	};
</script>

<style lang="scss">
	.page {
		background-color: #f5f5f5;
		height: 100vh;

		.search-bar {
			height: 70rpx;
			background-color: #fff;
			border-radius: 40rpx;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			margin: 0 20rpx;
			position: static;
			top: 10rpx;

			uni-icons {
				margin-right: 10rpx;
			}

			input {
				flex: 1;
				height: 60rpx;
				border: none;
				font-size: 32rpx;
			}
		}

		.printer-list {
			width: 95%;
			padding: 20rpx;
			margin-top: 15rpx;

			.printer-item {
				height: 120rpx;
				background-color: #fff;
				border-radius: 9rpx;
				box-shadow: 0rpx 0rpx 15rpx 0rpx rgba(0, 0, 0, 0.08);
				margin-bottom: 32rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 20rpx;

				.printer-name {
					font-weight: 400;
					font-size: 29rpx;
					color: #333333;
				}

				.store-name {
					font-weight: 400;
					font-size: 25rpx;
					color: #666666;
					margin-top: 10rpx;
				}
			}
		}

		.empty {
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;
			position: absolute;
			top: 50%;
			left: 50%;
			transform: translate(-50%, -50%);

			image {
				width: 200rpx;
				margin-bottom: 20rpx;
			}

			text {
				font-size: 24rpx;
			}
		}
	}
</style>